setwd("C:/Users/tim/Documents/Distance Covariance Project/distancecovariance/R Code/")
#setwd("C:/Documents and Settings/ORIE user1/My Documents/Distance Covariance Project/R Code/")
#setwd("C:/Documents and Settings/1083117/My Documents/Distance Covariance Project/R Code")
#optional to get the analysis running...
source("Functions/source.all.R")
source.all() #this will produce errors if there are any uninstalled packages
install.all.packages() #install any missing packages
source.all() #source again...

###################################

##set up our paramters here
d = 3 #dimension
#index of our 'independent' variable
indep.var = 1 
#knowledge of some other variable(s) makes this independent with indep.var
conditionally.independent.var = 2 
#always has some dependency with indep.var
dependent.var = 3
#we'll use one coefficient of dependence for this basic simulation
correlation1 = .8
correlation2 = 0.3
#number of samples to generate
n=1000
#mean of our distriution
mean = 0
##end parameter set-up
###################################

omega=matrix(rep(1,d^2),d)

for (j in 1:d){ #columns
	for (k in 1:d){
		if (j == k){
			omega[j,k]=1
		}else{
			if ((j<k & k<3) | (k<j & j < 3)){
				omega[j,k]=0
			}else{
				if((k>j & j>1) | (j>k & k>1)){
					omega[j,k]=correlation1
				}else{
					omega[j,k]=correlation2
				}
			}
		}	
	}
}

#invert
sigma=solve(omega)
#generate_a_multi-d_normal_distributino
x=as.data.frame(rmvnorm(n,rep(mean,d),sigma))
names(x)=c("Conditionally_Independent_Var_1","Conditionally_Independent_Var_2","Conditionally_Dependent_Var_3")
#some_plots
pairs(x)
a=as.data.frame(rep(1,100))
names(a)=c("first_element")
#test index 1
#test_for_independence among our conditionally independent variables
Y = x$Conditionally_Independent_Var_1
X = x$Conditionally_Independent_Var_2
dcov.test_= dcov.test(X,Y,R=199)
dcov.test.pvalue=dcov.test$p.value
dcov.test.pvalue
cor.test(X,Y,method="pearson")$p.value

#test index 2
#test_for_independence among our conditionally dependent variable and a conditioanlly independent variable
Y = x$Conditionally_Independent_Var_1
X = x$Conditionally_Dependent_Var_3
dcov.test_= dcov.test(X,Y,R=199)
dcov.test.pvalue=dcov.test$p.value
dcov.test.pvalue
cor.test(X,Y,method="pearson")$p.value

#test index 3
#test_for_independence among conditionally dependent variable and the other conditioanlly independent variable
Y = x$Conditionally_Independent_Var_2
X = x$Conditionally_Dependent_Var_3
dcov.test_= dcov.test(X,Y,R=199)
dcov.test.pvalue=dcov.test$p.value
dcov.test.pvalue
cor.test(X,Y,method="pearson")$p.value

#test index 4
#test_for_independence among our conditionally dependent variables after linear regression
#X1|X2 indep. X2 
Y = lm(x$Conditionally_Independent_Var_1 ~ x$Conditionally_Independent_Var_2)$residuals
X = x$Conditionally_Independent_Var_2
dcov.test_= dcov.test(X,Y,R=199)
dcov.test.pvalue=dcov.test$p.value
dcov.test.pvalue
cor.test(X,Y,method="pearson")$p.value

#test index 5
#test_for_independence among our conditionally dependent variables after linear regression
#X1|X2 indep. X3 
Y = lm(x$Conditionally_Independent_Var_1 ~ x$Conditionally_Independent_Var_2)$residuals
X = x$Conditionally_Dependent_Var_3
dcov.test_= dcov.test(X,Y,R=199)
dcov.test.pvalue=dcov.test$p.value
dcov.test.pvalue
cor.test(X,Y,method="pearson")$p.value


